package com.oa.action;

import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;

import org.jbpm.api.ProcessDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.oa.entity.Template;
import com.oa.service.LiuChengDingYiService;
import com.oa.service.TemplateService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @author FengMengHuo
 * 模板申请Action
 */
@Controller
@Scope("prototype")
public class TemplateAction extends ActionSupport implements ModelDriven<Template>{
	private static final long serialVersionUID = 1L;
	private Template template = new Template();
	// 注入模板服务类
	@Resource(name="templateService")
	private TemplateService templateService ;
	// 注入流程定义服务类
	@Resource(name="liuChengDingYiService")
	private LiuChengDingYiService liuChengDingYiService;
	
	
	//申请模板列表
	public String list(){
		List<Template> templateList =  templateService.findTemplateList();
		ActionContext.getContext().put("templateList", templateList);
		return "list";
	}
	//申请模板页面
	public String addPage(){
		// 获取所有最新的流程定义列表
		List<ProcessDefinition> pdList = liuChengDingYiService.findLiuChengDingYiList();
		ActionContext.getContext().put("pdList", pdList);
		return "savePage";
	}
	//添加申请模板
	public String add(){
		templateService.addTemplate(template);
		return "toList";
	}
	//申请模板的修改页面
	public String updatePage(){
		template = templateService.findTemplateById(template);
		ActionContext.getContext().put("template", template);
		List<ProcessDefinition> pdList = liuChengDingYiService.findLiuChengDingYiList();
		ActionContext.getContext().put("pdList", pdList);
		System.out.println(template.getTemplate_name()+template.getUploadPath()+template.getUnique_id());
		return "savePage";
	}
	//修改申请模板
	public String update(){
		templateService.updateTemplate(template);
		return "toList";
	}
	//删除申请模板
	public String delete(){
		templateService.deleteTemplateById(template);
		return "toList";
	}
	//模板下载
	public String download(){
		InputStream inputStream = templateService.downloadTemplate(template);
		template.setInputStream(inputStream);
		return "download";
	}
	
	
	
	
	
	
	// 返回模型
	public Template getModel() {
		return template;
	}

}
